/*
 * $Id: FunctionInterceptor.java 1471 2011-08-16 01:58:42Z abie $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 * 
 * Accentrix Company Limited. ("Accentrix") retains copyright on all text, source
 * and binary code contained in this software and documentation. Accentrix grants
 * Licensee a limited license to use this software, provided that this copyright
 * notice and license appear on all copies of the software. The software source
 * code is provided for reference purposes only and may not be copied, modified 
 * or distributed.
 * 
 * THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS," WITHOUT ANY WARRANTY OF
 * ANY KIND UNLESS A SEPARATE WARRANTIES IS PURCHASED FROM ACCENTRIX AND REMAINS
 * VALID.  ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ACCENTRIX SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING THE
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * IN NO EVENT WILL ACCENTRIX BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
 * USE OF OR INABILITY TO USE SOFTWARE, EVEN IF ACCENTRIX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */
package com.snowleopard.common.interceptor;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class FunctionInterceptor extends AbstractInterceptor {
    private static final long serialVersionUID = 2415136498381198824L;

    private final static Logger log = LoggerFactory.getLogger(FunctionInterceptor.class);

    public static final String MENU_FUNC = "menu_func";

    public FunctionInterceptor() {
        log.info("new FunctionInterceptor()");
    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        log.info("intercept '" + invocation.getProxy().getNamespace() + "/"
                + invocation.getProxy().getActionName() + "' { ");

        ActionContext actionContext = invocation.getInvocationContext();

        // get requested function
        Map<String, Object> params = actionContext.getParameters();

        Object requestedFunction = findFunctionParameter(params, MENU_FUNC);

        if (requestedFunction != null) {
            log.info("applied request function={}", requestedFunction);

            // save it in session
            Map<String, Object> session = actionContext.getSession();

            if (session != null) {
                synchronized (session) {
                    session.put(MENU_FUNC, requestedFunction.toString());
                }
            }
        }

        // finally, execute the Interceptor chain
        final String result = invocation.invoke();

        log.info("intercept } ");
        return result;
    }

    private Object findFunctionParameter(Map<String, Object> params, String parameterName) {
        Object requestedFunction = params.remove(parameterName);
        if (requestedFunction != null && requestedFunction.getClass().isArray()
                && ((Object[]) requestedFunction).length == 1) {
            requestedFunction = ((Object[]) requestedFunction)[0];

            log.info(parameterName + "=" + requestedFunction);
        }
        return requestedFunction;
    }

}
